﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace Waker.Common
{
    public class Loger
    {
        public Loger()
        {
            if (!Directory.Exists(@"c:\WakerLog"))
            {
                Directory.CreateDirectory(@"c:\WakerLog");
            }

        }

        public void Log(string message)
        {
            var logFile = Path.Combine(@"c:\WakerLog", DateTime.Today.ToString("yyyyMMdd")) + ".txt";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("==================================");
            sb.Append(DateTime.Now);
            sb.Append(":");
            sb.AppendLine(message);
            sb.AppendLine("");

            using (FileStream fs = new FileStream(logFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                var sr = new StreamReader(fs, Encoding.Default);
                var olds = sr.ReadToEnd();
                var newBytes = Encoding.Default.GetBytes(sb.ToString());
                var ol = Encoding.Default.GetBytes(olds).Count();
                var nl = newBytes.Count();
                fs.Write(newBytes, 0, nl);
            }

        }


        public void LogException(Exception ex)
        {
            var logFile = Path.Combine(@"c:\WakerLog", DateTime.Today.ToString("yyyyMMdd")) + ".txt";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("==================================");
            sb.Append(DateTime.Now);
            sb.Append(":");
            sb.AppendLine(ex.ToString());
            sb.AppendLine("");
            //File.AppendAllText(logFile, sb.ToString());
            using (FileStream fs = new FileStream(logFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                var sr = new StreamReader(fs, Encoding.Default);
                var olds = sr.ReadToEnd();
                var newBytes = Encoding.Default.GetBytes(sb.ToString());
                var ol = Encoding.Default.GetBytes(olds).Count();
                var nl = newBytes.Count();
                fs.Write(newBytes, 0, nl);
            }
        }

        public void LogException(string message)
        {
            var logFile = Path.Combine(@"c:\WakerLog", DateTime.Today.ToString("yyyyMMdd")) + ".txt";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("==================================");
            sb.Append(DateTime.Now);
            sb.Append(":");
            sb.AppendLine(message);
            sb.AppendLine("");
            //File.AppendAllText(logFile, sb.ToString());
            using (FileStream fs = new FileStream(logFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                var sr = new StreamReader(fs, Encoding.Default);
                var olds = sr.ReadToEnd();
                var newBytes = Encoding.Default.GetBytes(sb.ToString());
                var ol = Encoding.Default.GetBytes(olds).Count();
                var nl = newBytes.Count();
                fs.Write(newBytes, 0, nl);
            }
        }
    }
}
